/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.cib.fintech.entrustedinvestment.system.service.impl;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import com.cib.fintech.entrustedinvestment.biz.repository.TransferOrderRepository;
import com.cib.fintech.entrustedinvestment.system.domain.Visits;
import com.cib.fintech.entrustedinvestment.repository.LogRepository;
import com.cib.fintech.entrustedinvestment.system.repository.VisitsRepository;
import com.cib.fintech.entrustedinvestment.system.service.VisitsService;
import com.cib.fintech.entrustedinvestment.utils.StringUtils;
import com.cib.fintech.entrustedinvestment.utils.enums.TransferOrderStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
import javax.servlet.http.HttpServletRequest;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhangqingkun
* @date 2020-10-14
**/
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true,rollbackFor = Exception.class)
public class VisitsServiceImpl implements VisitsService {

    private final VisitsRepository visitsRepository;
    private final LogRepository logRepository;
    @Autowired
    private TransferOrderRepository transferOrderRepository;

    public VisitsServiceImpl(VisitsRepository visitsRepository,LogRepository logRepository){
        this.visitsRepository = visitsRepository;
        this.logRepository = logRepository;
    }

    @Override
    public void save() {
        LocalDate localDate = LocalDate.now();
        ZoneId zoneId = ZoneId.systemDefault();
        Visits visits = visitsRepository.findByDate(Date.from(localDate.atStartOfDay(zoneId).toInstant()));
        if(visits == null){
            visits =new Visits();
            visits.setWeekDay(StringUtils.getWeekDay());
            visits.setPvCounts(1L);
            visits.setIpCounts(1L);
            visits.setDate(Date.from(localDate.atStartOfDay(zoneId).toInstant()));
            visitsRepository.save(visits);
        }
    }

    @Override
    public void count(HttpServletRequest request) {
        LocalDate localDate = LocalDate.now();
        ZoneId zoneId = ZoneId.systemDefault();
        Visits visits = visitsRepository.findByDate(Date.from(localDate.atStartOfDay(zoneId).toInstant()));
        if(visits == null){
            visits =new Visits();
            visits.setWeekDay(StringUtils.getWeekDay());
            visits.setPvCounts(1L);
            visits.setIpCounts(1L);
            visits.setDate(Date.from(localDate.atStartOfDay(zoneId).toInstant()));
            visitsRepository.save(visits);
        }
    }

    @Override
    public Object get() {
        Map<String,Object> map =new HashMap<>(12);
        List<Map<String,String>> dayVisitsList =transferOrderRepository.findDayCountAndSumByPaymentDateGroupByStatus(DateUtil.format(new Date(),"yyyyMMdd"));
        dayVisitsList.stream().forEach(visitMap->{
            if (TransferOrderStatusEnum.SUCCESS.getCode().equals(visitMap.get("status"))){
                map.put("dayCountS",visitMap.get("count"));
                map.put("dayAmountS",visitMap.get("amount"));
            }else if(TransferOrderStatusEnum.FAIL.getCode().equals(visitMap.get("status"))){
                map.put("dayCountF",visitMap.get("count"));
                map.put("dayAmountF",visitMap.get("amount"));
            }else if(TransferOrderStatusEnum.PROCESS.getCode().equals(visitMap.get("status"))){
                map.put("dayCountP",visitMap.get("count"));
                map.put("dayAmountP",visitMap.get("amount"));
            }
        });
        List<Map<String,String>> weekVisitsList =transferOrderRepository.findweekCountAndSumByPaymentDateGroupByStatus(DateUtil.format(DateUtil.lastWeek(),"yyyyMMdd"));
        weekVisitsList.stream().forEach(visitMap->{
            if (TransferOrderStatusEnum.SUCCESS.getCode().equals(visitMap.get("status"))){
                map.put("weekCountS",visitMap.get("count"));
                map.put("weekAmountS",visitMap.get("amount"));
            }else if(TransferOrderStatusEnum.FAIL.getCode().equals(visitMap.get("status"))){
                map.put("weekCountF",visitMap.get("count"));
                map.put("weekAmountF",visitMap.get("amount"));
            }else if(TransferOrderStatusEnum.PROCESS.getCode().equals(visitMap.get("status"))){
                map.put("weekCountP",visitMap.get("count"));
                map.put("weekAmountP",visitMap.get("amount"));
            }
        });
        map.put("dayCountS","1");
        map.put("dayAmountS","1");
        return map;
    }
}